﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CRM.Entity;
using CRM.BLL;
using System.Text;

namespace CRM.Web
{
    public partial class Left : Common.PageBase
    {
        string strLevelOne = "<table id='LevelOne_{0}' border='0' cellpadding='0' cellspacing='0' class='tLeftMenuLevelOne'><tr><td class='titleleft'></td><td class='titlemiddle'><div class='lefttitle'><div><span>{1}</span></div></div></td><td class='titleright'></td></tr></table>";
        string strLevelTwoPre = "<div class='divLeftMenuLevelTwo' id='LevelTwo_{0}' ><table border='0' cellpadding='0' cellspacing='0' class='tLeftMenuLevelTwo' runat='server'>";
        string strLevelTwoEnd = "</table></div>";

        string strLevelTwo = "<tr><td class='titleleft'></td><td class='titlemiddle'><div class='lefttitle'><div><a href='{1}' target='{2}'>{0}</a></div></div></td><td class='titleright'></td></tr>";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblUserName.Text = _userLoginInfo.UserEntity.USERNAME;
                lblLoginName.Text = _userLoginInfo.UserEntity.LOGINNAME;
                lblAgentID.Text = _userLoginInfo.UserEntity.SEATID;
                lblExt.Text = _userLoginInfo.UserEntity.EXT;
                lblDepartmentName.Text = UserLoginInfo.GetDepartmentName(_userLoginInfo);
                MenuBinding();
                //TreeBinding();
            }
        }

        private void MenuBinding()
        {
            StringBuilder strb = new StringBuilder();

            if (UserLoginInfo.IsAdministrator())
            {
                T_MENU_BLL bll = new T_MENU_BLL();
                List<T_MENU_Entity> list = bll.GetList("", " SORTODER desc, MENUID asc ");
                RecursiveMenuBinding(list, "0", strb);
            }
            else
            {
                UserLoginInfo loginInfo = UserLoginInfo.GetLoginInfo();
                List<V_ROLEMENU_Entity> list = loginInfo.RoleMenuList;
                RecursiveMenuBinding(list, "0", strb);
            }


            divLeftMenuBody.InnerHtml = strb.ToString();

        }


        private void RecursiveMenuBinding(List<T_MENU_Entity> list, string parentID, StringBuilder strb)
        {
            List<T_MENU_Entity> subList = list.FindAll(delegate(T_MENU_Entity en) { if (en.PARENTMENUID.ToString() == parentID) return true; else return false; });
            foreach (T_MENU_Entity entity in subList)
            {
                if (entity.MENUTYPE.ToString() == ((int)MenuType.目录).ToString())
                {
                    strb.Append(String.Format(strLevelOne, entity.MENUID, entity.MENUNAME));
                    strb.Append(String.Format(strLevelTwoPre, entity.MENUID));
                    //strb.Append(String.Format("d.add({0},{1},'{2}','');", entity.MENUID.ToString(), entity.PARENTMENUID.ToString(), entity.MENUNAME.ToString()));
                    RecursiveMenuBinding(list, entity.MENUID.ToString(), strb);
                    strb.Append(String.Format(strLevelTwoEnd));
                }
                else if (entity.MENUTYPE.ToString() == ((int)MenuType.页面).ToString())
                {
                    string url = entity.PAGEPATH.TrimEnd('/') + "/" + entity.PAGENAME;
                    strb.Append(String.Format(strLevelTwo, entity.MENUNAME, url, entity.MENUTARGET));
                    //strb.Append(String.Format("d.add({0},{1},'{2}','{3}', '{2}', '{4}');", entity.MENUID.ToString(), entity.PARENTMENUID.ToString(), entity.MENUNAME.ToString(), url, entity.MENUTARGET.Trim()));
                }
            }
        }

        private void RecursiveMenuBinding(List<V_ROLEMENU_Entity> list, string parentID, StringBuilder strb)
        {
            List<V_ROLEMENU_Entity> subList = list.FindAll(delegate(V_ROLEMENU_Entity en) { if (en.PARENTMENUID.ToString() == parentID) return true; else return false; });
            foreach (V_ROLEMENU_Entity entity in subList)
            {
                if (entity.MENUTYPE.ToString() == ((int)MenuType.目录).ToString())
                {
                    strb.Append(String.Format(strLevelOne, entity.MENUID, entity.MENUNAME));
                    strb.Append(String.Format(strLevelTwoPre, entity.MENUID));
                    //strb.Append(String.Format("d.add({0},{1},'{2}','');", entity.MENUID.ToString(), entity.PARENTMENUID.ToString(), entity.MENUNAME.ToString()));
                    RecursiveMenuBinding(list, entity.MENUID.ToString(), strb);
                    strb.Append(String.Format(strLevelTwoEnd));
                }
                else if (entity.MENUTYPE.ToString() == ((int)MenuType.页面).ToString())
                {
                    string url = entity.PAGEPATH.TrimEnd('/') + "/" + entity.PAGENAME;
                    strb.Append(String.Format(strLevelTwo, entity.MENUNAME, url, entity.MENUTARGET));
                    //strb.Append(String.Format("d.add({0},{1},'{2}','{3}', '{2}', '{4}');", entity.MENUID.ToString(), entity.PARENTMENUID.ToString(), entity.MENUNAME.ToString(), url, entity.MENUTARGET.Trim()));
                }
            }
        }


        private void TreeBinding()
        {
            string urlpath = GetServerPath();
            StringBuilder strb = new StringBuilder();
            strb.Append("d = new dTree('d');");
            strb.Append("d.add(0,-1,'菜单','');");

            if (UserLoginInfo.IsAdministrator())
            {
                T_MENU_BLL bll = new T_MENU_BLL();
                List<T_MENU_Entity> list = bll.GetList("", " SORTODER desc, MENUID asc ");
                RecursiveTreeBinding(list, "0", strb);
            }
            else
            {
                UserLoginInfo loginInfo = UserLoginInfo.GetLoginInfo();
                List<V_ROLEMENU_Entity> list = loginInfo.RoleMenuList;
                RecursiveTreeBinding(list, "0", strb);
            }
            strb.Append("document.getElementById('leftTree').innerHTML = d;");
            strb.Append("d.closeAll();");

            Page.ClientScript.RegisterStartupScript(this.GetType(), "tree", strb.ToString(), true);
        }

        private void RecursiveTreeBinding(List<T_MENU_Entity> list, string parentID, StringBuilder strb)
        {
            List<T_MENU_Entity> subList = list.FindAll(delegate(T_MENU_Entity en) { if (en.PARENTMENUID.ToString() == parentID) return true; else return false; });
            foreach (T_MENU_Entity entity in subList)
            {
                if (entity.MENUTYPE.ToString() == ((int)MenuType.目录).ToString())
                {
                    strb.Append(String.Format("d.add({0},{1},'{2}','');", entity.MENUID.ToString(), entity.PARENTMENUID.ToString(), entity.MENUNAME.ToString()));
                    RecursiveTreeBinding(list, entity.MENUID.ToString(), strb);
                }
                else if (entity.MENUTYPE.ToString() == ((int)MenuType.页面).ToString())
                {
                    string url = entity.PAGEPATH.TrimEnd('/') + "/" +　entity.PAGENAME;
                    strb.Append(String.Format("d.add({0},{1},'{2}','{3}', '{2}', '{4}');", entity.MENUID.ToString(), entity.PARENTMENUID.ToString(), entity.MENUNAME.ToString(), url, entity.MENUTARGET.Trim()));
                }
            }
        }

        private void RecursiveTreeBinding(List<V_ROLEMENU_Entity> list, string parentID, StringBuilder strb)
        {
            List<V_ROLEMENU_Entity> subList = list.FindAll(delegate(V_ROLEMENU_Entity en) { if (en.PARENTMENUID.ToString() == parentID) return true; else return false; });
            foreach (V_ROLEMENU_Entity entity in subList)
            {
                if (entity.MENUTYPE.ToString() == ((int)MenuType.目录).ToString())
                {
                    strb.Append(String.Format("d.add({0},{1},'{2}','');", entity.MENUID.ToString(), entity.PARENTMENUID.ToString(), entity.MENUNAME.ToString()));
                    RecursiveTreeBinding(list, entity.MENUID.ToString(), strb);
                }
                else if (entity.MENUTYPE.ToString() == ((int)MenuType.页面).ToString())
                {
                    string url = entity.PAGEPATH.TrimEnd('/') + "/" + entity.PAGENAME;
                    strb.Append(String.Format("d.add({0},{1},'{2}','{3}', '{2}', '{4}');", entity.MENUID.ToString(), entity.PARENTMENUID.ToString(), entity.MENUNAME.ToString(), url, entity.MENUTARGET.Trim()));
                }
            }
        }

        private string GetServerPath()
        {
            string url = Request.Url.AbsoluteUri.Replace("TreeList.aspx", "");
            return url;
        }
    }
}
